using FileStorage.Common;
using FileStorage.Common.Extensions;
using FileStorage.Common.Filters;
using FileStorage.Models.Entity.Mapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace FileStorage
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // get the System Configs
            Configuration.GetSectionValue<SystemConfig>();
            Configuration.GetSectionValue<ServerConfig>();
            Configuration.GetSectionValue<AccountConfig>();
            Configuration.GetSectionValue<AmazonS3Config>();

            //services.AddAutoMapper(typeof(MapperConfig));

            services.AddCors();

            //services.AddAntiforgery(options => { options.SuppressXFrameOptionsHeader = true; });

            services.AddControllers(options =>
            {
                options.Filters.Add<ExceptionFilter>();
            }).AddNewtonsoftJson(options =>
            {
                options.UseCamelCasing(true);
            });

            services.Configure<ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = context => context.ModelState.IsValid ? null : ServerResponse.No(context.ModelState.GetFirstOrDefaultError());
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            });

            services.AddDistributedMemoryCache();
            services.AddSession(options => { options.IdleTimeout = TimeSpan.FromDays(365); });

            services.AddSingleton<RSAKeyUtil>();
            services.AddSingleton<AmazonS3Util>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            //add the log4net
            loggerFactory.AddLog4Net();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSession();

            app.UseMustLogin();

            app.UseStaticFiles();
         
            //add the log directory as static, so we can view the log directory
            app.UseFileServer(new FileServerOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Logs")),
                RequestPath = new PathString("/Log"),
                EnableDirectoryBrowsing = true
            });

            app.UseCors(
               options => options.WithOrigins("*").AllowAnyHeader().AllowAnyMethod()
            );

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });
        }
    }
}
